package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.domain.Orders;
import com.itheima.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单
 */
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;


    /**
     * 提交订单，购买
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R submitOrder(@RequestBody Orders orders) {
        //13812345678
        ordersService.submit(orders);

        return R.success("下单成功");
    }


    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R userPageOrder(Integer page,
                           Integer pageSize) {
        //封装条件
        IPage<Orders> orderPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        //用户id
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        //分页
        ordersService.page(orderPage, wrapper);
        //获取值
        long total = orderPage.getTotal();
        List<Orders> records = orderPage.getRecords();
        //把值封装到map中
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("records", records);
        //返回值
        return R.success(orderPage);
    }


    @GetMapping("/page")
    public R mxPage(Integer page,
                    Integer pageSize,
                    String number,
                    Data beginTime,
                    Data endTime) {
        //封装条件
        IPage<Orders> orderPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        //用户id
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        wrapper.eq(Orders::getNumber, number);
        Orders orders = new Orders();


        //分页
        ordersService.page(orderPage, wrapper);
        //获取值
        long total = orderPage.getTotal();
        List<Orders> records = orderPage.getRecords();
        //把值封装到map中
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("records", records);


        return R.success(orderPage);
    }

}